// file Term.java
// 발생 텀별로 확율 조사
//=====================================

package z_app_lotto2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Term2 extends JPanel implements ActionListener{
	Data d;
	Vector data;
    JButton button;
    JTextArea doc;
    JLabel jLabel1;
    JScrollPane jScrollPane2;
    JTextField no;
    JPanel panel;
	int val[][];
	int termNo[][];


	public Term2(){
		initComponents();
		init();
	}

	public void init(){
		boolean find=false;
		String colName[] = {"번호","발생횟수", "확율"};
		String dd = new String();

		int no=0, base=0, first=0, term=0, totCnt=0;
		double rate=0.0;
		int termCnt=0;
		int outputCnt=0;
		doc.setText("");

		button.addActionListener(this);

		d = new Data("src/z_app_lotto2/lotto.dat");
		data = d.getData();

		val = new int[data.size()][8];
		termNo = new int[46][data.size()];

		// data compute
		for(int i=0 ; i<data.size(); i++){
			Vector tmp = (Vector)data.get(i);
			for(int j=0 ; j<tmp.size(); j++){
				val[i][j] = Integer.parseInt((String)tmp.get(j));
			}
		}

		// term
		for(no = 1; no < 46 ; no++){
			find = false;
			first = 0;
			termCnt=1;
			termNo[no][0] = no;
			outputCnt=0;
			for(int i=0 ; i<data.size() ; i++){
				for(int j=1 ; j<8 ; j++){
					if(val[i][j] == no){
						if(first == 0)
							first = val[i][0];
						else{
							termNo[no][termCnt] = first - val[i][0];
							first = val[i][0];
							termCnt++;
						}
						break;
					}
				}
			}
		}




		base = val[0][0] + 1;
		doc.append("\n\n\n");
		doc.append("base=" + base + "\n");

		// 최종 출현 횟차 검색
		for(no=1; no<46; no++){
			find = false;
			for(int i=0 ; i<data.size() && find==false; i++){
				for(int j=1 ; j<8;j++){
					if(val[i][j] == no){
						find=true;
						first = val[i][0];
						break;
					}
				}
			}

			term = base-first;
			totCnt = 0;
			for(int i=1; i<data.size() ; i++){
					if(termNo[no][i] == term){
						totCnt++;
					}
			}

			rate = (totCnt*1.0) / data.size() * 100;
			if(totCnt >2){
				dd = "" + no + " : " + first + ", " + term + ", [" + totCnt + "] [" + rate + "]\n";
				doc.append(dd);
				outputCnt++;
			}

		}
		doc.append("\n" + "count=" + outputCnt);
	}

	public void actionPerformed(ActionEvent e){
		init();
	}

    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        no = new javax.swing.JTextField();
        button = new javax.swing.JButton();
        panel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        doc = new javax.swing.JTextArea();

        setLayout(null);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("\ud604\uc7ac \ud68c\ucc28");
        add(jLabel1);
        jLabel1.setBounds(20, 10, 70, 16);


        add(no);
        no.setBounds(100, 10, 100, 21);

        button.setText("\ud655\uc778");
        add(button);
        button.setBounds(200, 10, 70, 25);

        panel.setLayout(null);


        doc.setColumns(25);
        doc.setRows(20);
        jScrollPane2.setViewportView(doc);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 40, 550, 500);

	}

	public static void main(String args[]){
		JFrame frame =new JFrame();
		Term2 t = new Term2();

		frame.setSize(600,600);
		frame.getContentPane().add(t);

		frame.setVisible(true);

	}




}
